/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package isvp.sax.handlers;

import isvp.sax.SAXHandler;
import org.w3c.dom.Document;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 *
 * @author jjophin
 */
public class IsvXMLHandler extends SAXHandler {
    private boolean descriptionTaken = false;
    private boolean descriptionStarted = false;
    private boolean UCImporterStarted = false;
    
    private final static String DESCRIPTION = "description";
    private final static String UCIMPORTER = "UCImporter";
    
    
    private void elementDetected(String qName, boolean startTag) {
        if (DESCRIPTION.equals(qName) && !descriptionTaken) {
            descriptionStarted = startTag;
            xmlString += getTagName(qName, startTag);
            if(!startTag) {
                descriptionTaken = true;
                xmlString += getTagName("content", !startTag);
            }            
        } else if (UCIMPORTER.equals(qName)) {
            UCImporterStarted = startTag;
            xmlString += getTagName(qName, startTag);
            if(!startTag){
                xmlString += getTagName("content", startTag);
                xmlString += getTagName("ISVPackage", startTag);
            }
        } else if ((descriptionStarted && !descriptionTaken) || UCImporterStarted) {
            xmlString += getTagName(qName, startTag);
        }        
    }
    
    private String getTagName(String localName, boolean startTag) {
        String tagName = "<" + (startTag ? "" : "/") + localName + ">";
        return tagName;
    }  
    
    public IsvXMLHandler() {
        initialize();
    }
    
    @Override
    public void initialize() {
        xmlString = "<ISVPackage>";
        descriptionTaken = false;
        descriptionStarted = false;
        UCImporterStarted = false;
    }
    /**
    * Receive notification of the start of an element.
    * @param namespaceURI - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
    * @param localName - The local name (without prefix), or the empty string if Namespace processing is not being performed.
    * @param qName - The qualified name (with prefix), or the empty string if qualified names are not available.
    * @param atts - The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
    * @throws SAXException - Any SAX exception, possibly wrapping another exception.
    */
    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        elementDetected(qName, true);
    }

    /**
    * Receive notification of character data inside an element.
    * @param ch - The characters.
    * @param start - The start position in the character array.
    * @param length - The number of characters to use from the character array.
    * @throws SAXException - Any SAX exception, possibly wrapping another exception.
    */
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if ((descriptionStarted && !descriptionTaken) || UCImporterStarted) {
            String strValue = new String(ch, start, length);
            xmlString += strValue;
        }
    }

  /**
   * Receive notification of the end of an element.
   * @param namespaceURI - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
   * @param localName - The local name (without prefix), or the empty string if Namespace processing is not being performed.
   * @param qName - The qualified name (with prefix), or the empty string if qualified names are not available.
   * @throws SAXException - Any SAX exception, possibly wrapping another exception.
   */
    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        elementDetected(qName, false);
    }
}
